import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:random_lottery_ticket/random_data.dart';
import 'package:provider/provider.dart';
import 'package:random_lottery_ticket/service.dart';
import 'random_data.dart';


/**
 *  正常随机
 */

class NormalRandomWidget extends StatefulWidget {
  @override
  _NormalRandomWidgetState createState() => _NormalRandomWidgetState();
}

class _NormalRandomWidgetState extends State<NormalRandomWidget> {
  RandomNumber rm;

  @override
  Widget build(BuildContext context) {
    rm = Provider.of<RandomNumber>(context);
    return Container(
        child: Column(
          children: [
            Padding(padding: EdgeInsets.fromLTRB(0, 30, 0, 0)),
            Text('普通随机',
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(60), color: Colors.red)),
            Padding(padding: EdgeInsets.fromLTRB(0, 30, 0, 0)),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('我要随机',
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(60), color: Colors.red)),
                IconButton(
                    icon: Icon(Icons.minimize),
                    color: Colors.red,
                    onPressed: () {
                        if(rm.number>1) {
                          rm.reduce();
                          print(rm.number);
                         }
                        }),
                Selector<RandomNumber,int>(	// 实现局部刷新
                  builder: (context,number,child) => Theme(
                    data: new ThemeData(
                        primaryColor: Colors.red, hintColor: Colors.red),
                    child: ConstrainedBox(
                      constraints: BoxConstraints(maxHeight: 50, maxWidth: 50),
                      child: TextField(
                        controller: TextEditingController(text: number.toString()),
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontSize: ScreenUtil().setSp(60), color: Colors.red),
                        readOnly: true,
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.all(10.0),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(15.0),
                          ),
                        ),
                      ),
                    ),
                  ),
                  selector: (context,model) => model.number,
                ),
                IconButton(
                    icon: Icon(Icons.add),
                    color: Colors.red,
                    onPressed: () {
                      if(rm.number<20) {
                        rm.increment();
                      }
                      print(rm.number);
                    }),
                Text('注',
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(60), color: Colors.red))
              ],
            ),
            Padding(padding: EdgeInsets.fromLTRB(0, 30, 0, 0)),
            Text('一夜暴富，今晚必中',
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(60), color: Colors.red)),
            Padding(padding: EdgeInsets.fromLTRB(0, 30, 0, 0)),
          ],
        ),
      );
  }
}



// 复式随机

class CompoundRandomWidget extends StatefulWidget {

  TextEditingController rbcontroller;
  TextEditingController bbcontroller;

  CompoundRandomWidget(this.rbcontroller, this.bbcontroller);

  @override
  _CompoundRandomWidgetState createState() => _CompoundRandomWidgetState();
}

class _CompoundRandomWidgetState extends State<CompoundRandomWidget> {
  Widget buildTextField(TextEditingController controller) {
    return SizedBox(
      height: ScreenUtil().setHeight(200),
      width: ScreenUtil().setWidth(200),
      child:TextField(
      controller: controller,
      maxLines: 1,//最大行数
      textAlign: TextAlign.center,//文本对齐方式
      style: TextStyle(fontSize: ScreenUtil().setSp(60), color: Colors.red),//输入文本的样式
      inputFormatters: [FilteringTextInputFormatter.allow(RegExp("[0-9]"))],//允许的输入格式
      onChanged: (val) {//内容改变的回调
        print(val);
      },
      decoration: InputDecoration(
        contentPadding: EdgeInsets.all(10.0),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(15.0),
        ),
      ),
    ),);
  }


  @override
  Widget build(BuildContext context) {
    int m = int.parse(widget.rbcontroller.text, onError: (source) => 0);
    int n = int.parse(widget.bbcontroller.text, onError: (source) => 0);
    var piece = RandomService().getOnePiece(m, 5)*RandomService().getOnePiece(n, 2);
    var price = piece*2;
    return SingleChildScrollView(
      child: Column(
        children: [
          Padding(padding: EdgeInsets.fromLTRB(0, 30, 0, 0)),
          Text('复式随机',style: TextStyle(
               fontSize: ScreenUtil().setSp(60), color: Colors.red)),
          Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 0)),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('红球：',style: TextStyle(
                  fontSize: ScreenUtil().setSp(60), color: Colors.red)),
               buildTextField(widget.rbcontroller),
            ],
          ),

          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('蓝球：',style: TextStyle(
                  fontSize: ScreenUtil().setSp(60), color: Colors.blue)),
              buildTextField(widget.bbcontroller),
            ],
          ),
          Text('共'+piece.ceil().toString()+"注，约需"+price.ceil().toString()+"元",style: TextStyle(
              fontSize: ScreenUtil().setSp(60), color: Colors.red)),
        ],

      ),
    );
  }
}



















class CustomizeWidgets {
  Widget danTuoRandomWidget() {
    return Container(
      height: 310,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('胆拖随机'),
        ],
      ),
    );
  }

  Widget removeRandomWidget() {
    return Container(
      height: 310,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('随机排除'),
        ],
      ),
    );
  }
}
